<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>
  <script>
    function crackSafe(n, k) {
      const set = new Set(); //dfs必会把全部n位密码 遍历到; 这是关键
      const start = '0'.repeat(n - 1);
      let res = '';
      function dfs(pre) {
        for (let i = 0; i < k; i++) {
          const cur = pre + i;
          if (set.has(cur)) continue;
          set.add(cur);
          dfs(cur.slice(1));
          res = i + res;
        }
      }
      dfs(start)
      return start + res;
    };
    console.log(crackSafe(n = 2, k = 2))
  </script>
</body>

</html>